<?php

namespace app\dynamic\controller;

use app\AdminRest;
use app\dynamic\model\CardTimeline;
use app\dynamic\model\Cate;
use think\App;

class AdminCate extends AdminRest
{

    protected $model;

    // 继承 验证用户登陆
    public function __construct ( App $app )
    {
        parent::__construct( $app );

        $this->model = new Cate();
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-24 10:34
     * @功能说明:列表
     */
    public function cateList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];

        }
        $data = $this->model->cateIist($dis,$input['limit']);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-24 10:41
     * @功能说明:添加
     */
    public function cateAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->model->cateAdd($input);

        return $this->success($res);


    }

    /**
     * @author chenniang
     * @DataTime: 2020-07-24 10:43
     * @功能说明:编辑
     */
    public function cateUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->model->cateUpdate($dis,$input);

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-24 10:54
     * @功能说明:上下架
     */
    public function statusUpdate(){

        $input = $this->_input;

        $timeline_model = new CardTimeline();

        $where[] = ['status','>',-1];

        $where[] = ['cate_id','=',$input['id']];

        $where[] = ['uniacid','=',$this->_uniacid];

        $time_line = $timeline_model->where($where)->column('title');

        if(!empty($time_line)&&$input['status']!=1){

            $time_line = implode(',',$time_line);

            $this->errorMsg('该动态正在被使用'.$time_line);
        }

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->model->cateUpdate($dis,['status'=>$input['status']]);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-24 11:08
     * @功能说明:回显
     */
    public function cateInfo(){

        $input = $this->_param;

        $data  = $this->model->cateInfo(['id'=>$input['id']]);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-24 11:42
     * @功能说明:下拉框
     */
    public function cateSelect(){

        $where[] = ['status','=',1];

        $where[] = ['uniacid','=',$this->_uniacid];

        $data = $this->model->cateSelect($where);

        return $this->success($data);
    }



}
